import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class AvatarWidget extends StatelessWidget {
  final String imageUrl;
  final double? size;
  final double? width;
  final double? height;
  final double? circular;

  const AvatarWidget(
      {super.key,
      required this.imageUrl,
      this.size,
      this.width,
      this.height,
      this.circular});

  @override
  Widget build(BuildContext context) {
    print(imageUrl.isEmpty
        ? "图片地址为空: $imageUrl。将会使用本地默认头像！"
        : "图片地址不为空: $imageUrl。");
    return SizedBox(
      width: size ?? width,
      height: size ?? height,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(circular!),
        // 设置圆角大小
        child: imageUrl.isEmpty || imageUrl == ("null")
            ? Image.asset('assets/unlogin.png')
            : CachedNetworkImage(
                imageUrl: imageUrl,
                placeholder: (context, url) =>
                    const CircularProgressIndicator(),
                cacheKey: imageUrl,
                errorWidget: (context, url, error) => const Icon(Icons.error),
                fit: BoxFit.cover,
              ),
      ),
    );
  }
}
